package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.exception.BusinessRuntimeException;
import com.itheima.pojo.Permission;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service
@Slf4j
public class PermissionServiceImpl implements PermissionService {
	@Autowired
	PermissionDao permissionDao;
	@Override
	public PageResult findByPage(QueryPageBean queryPageBean) {
		log.debug("service:" + queryPageBean);
		//开始分页
		PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
		//条件查询
		Page<Permission> page = permissionDao.findByCondition(queryPageBean.getQueryString());
		log.debug("page{" + page + "}");
		//封装PageResult
		return new PageResult(page.getTotal(), page);
	}

	@Override
	public void add(Permission permission) {
		log.debug("PermissionService{" + permission + "}");
		permissionDao.add(permission);
	}

	@Override
	public Permission findById(Integer id) {
		return permissionDao.findById(id);
	}

	@Override
	public void edit(Permission permission) {
		permissionDao.edit(permission);
	}

	@Override
	public void delById(Integer id) {
		//查询当前权限是否和角色表关联
		long count = permissionDao.findCountByPermissionId(id);
		if (count > 0) {
			log.debug("当前权限被引用,不能删除");
			throw new BusinessRuntimeException("当前权限被引用,不能删除");
		}
		permissionDao.delById(id);
		log.debug("删除成功");
	}

 @Override
 public List<Permission> findAll() {
		return permissionDao.findAll();
 }

}
